﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DAO
{
    public class ManagerDAO
    {
        public static DataClasses1DataContext Data = new DataClasses1DataContext();
        
        public static List<MEMBER> GetAllManager()
        {
            var query = from manager in Data.MEMBERs where ((manager.ACCOUNT.VISIBLE==true)&&(manager.MEMBER_STYLE==2)) select manager;
            List<MEMBER> ListManager = new List<MEMBER>();
            foreach (MEMBER manager in query)
                ListManager.Add(manager);
            return ListManager;
        }
        
        public static MEMBER GetManager(int IDManager)
        {
            MEMBER Manager=Data.MEMBERs.Single(manager => manager.IDMEMBER == IDManager);
            return Manager;
        }
        
        public static MEMBER GetManager(string UserName, string PassWord)
        {
            MEMBER manager=Data.MEMBERs.Single(Manager => (Manager.ACCOUNT.USERNAME.Trim() == UserName) && (Manager.ACCOUNT.PASSWORD.Trim() == PassWord));
            return manager;
        }

        public static bool IsExisting(int IDManager)
        {
            bool existing=Data.MEMBERs.Any(manager => ((manager.IDMEMBER == IDManager)&&(manager.ACCOUNT.VISIBLE==true)&&(manager.MEMBER_STYLE==2)));
            return existing;
        }

        public static bool IsExisting(string UserName,string PassWord)
        {
            bool existing=Data.MEMBERs.Any(Manager => ((Manager.ACCOUNT.USERNAME.Trim() == UserName) && (Manager.ACCOUNT.PASSWORD.Trim() == PassWord)&&(Manager.ACCOUNT.VISIBLE==true)&&(Manager.MEMBER_STYLE==2)));
            return existing;
        }

        public static bool IsExisting(string UserName)
        {
            bool existing = Data.MEMBERs.Any(Manager => ((Manager.ACCOUNT.USERNAME.Trim() == UserName)&&(Manager.ACCOUNT.VISIBLE==true)&&(Manager.MEMBER_STYLE==2)));
            return existing;
        }

        public static bool updateMember(MEMBER mMember)
        {
            try
            {
                MEMBER member = Data.MEMBERs.Single(temp => temp.ACCOUNT.USERNAME == mMember.ACCOUNT.USERNAME);
                member.ACCOUNT.ADDRESS = mMember.ACCOUNT.ADDRESS;
                member.ACCOUNT.CMND = mMember.ACCOUNT.CMND;
                member.ACCOUNT.FULLNAME = mMember.ACCOUNT.FULLNAME;
                member.ACCOUNT.PASSWORD = mMember.ACCOUNT.PASSWORD;
                member.ACCOUNT.PHONE_NUMBER = mMember.ACCOUNT.PHONE_NUMBER;
                Data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
